package com.shell.governor.authorization.controller;

import com.shell.bee.base.annotation.Logging;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.entity.mvc.RetMsg;
import com.shell.bee.mvc.controller.BaseController;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.governor.authorization.po.PubResource;
import com.shell.governor.authorization.po.PubRole;
import com.shell.governor.authorization.service.PubRoleService;
import com.shell.governor.authorization.vo.AuthTmplRoleVo;
import com.shell.governor.modules.po.PubApplication;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

@RestController
public class PubRoleController extends BaseController {
	@Autowired
	private PubRoleService roleService;

	@RequestMapping
	@Logging(module = "��������", methods = "��������")
	public RetMsg addRole(PubRole role, HttpServletRequest request) throws Exception {
		return this.roleService.saveOrUpdateRole(role);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������")
	public RetMsg updateRole(PubRole role, HttpServletRequest request) throws Exception {
		return this.roleService.saveOrUpdateRole(role);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������")
	public RetMsg deleteRole(PubRole role, HttpServletRequest request) throws Exception {
		return this.roleService.deleteRole(role);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������")
	public List<PubRole> selectRole(PageLimit limit, PubRole role, HttpServletRequest request) throws Exception {
		return this.roleService.selectRolesByPage(limit, role);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "����������������������")
	public List<PubRole> selectResourceRole(PubRole role, HttpServletRequest request) throws Exception {
		return this.roleService.selectResourceRole(role);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "����������������������")
	public RetMsg authRoleToRole(String roleId, String roleIds) {
		return this.roleService.authRoleToRole(roleId, roleIds);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "������������")
	public RetMsg updateRoleStateWithCascade(String roleId, String roleState, HttpServletRequest request)
			throws Exception {
		String msg = roleState.equals("U") ? "����" : "����";
		if (this.roleService.updateRoleStateWithCascade(roleId, roleState) > 0) {
			return initRetMsg("success", msg + "����������");
		}
		return initRetMsg("error", msg + "����������");
	}

	@RequestMapping
	@Logging(module = "��������", methods = "����������������������")
	public List<AuthTmplRoleVo> selectRole4AuthUser(String appId, String entId, String userId) {
		return this.roleService.selectRole4AuthUser(appId, userId, entId, CommonDataUtil.getUserId(),
				CommonDataUtil.getCurEntId());
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������������������������")
	public RetMsg checkRoleCode(PubRole role) throws Exception {
		return initRetMsg("success", false, Boolean.valueOf(this.roleService.checkCodeUnique(role)));
	}

	@RequestMapping
	@Logging(module = "��������", methods = "����roleIds������������-��������������")
	public List<PubResource> selectResourceTreeData4Role(String roleId, String type, String bizId) throws Exception {
		return this.roleService.selectResourceTreeData4Role(roleId, type, bizId);
	}

	@RequestMapping
	@Logging(module = "��������", methods = "��������������������������������")
	public List<PubApplication> getApp4Admin() throws Exception {
		return this.roleService.selectAppByAdminRole(CommonDataUtil.getUserId());
	}

	@RequestMapping
	@Logging(module = "��������", methods = "������������")
	public List<PubRole> selectAllRole(String roleName) {
		Example example = new Example(PubRole.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("roleState", "E");
		if (StringUtil.notEmpty(roleName)) {
			criteria.andLike("roleName", StringUtil.getParameterLike(roleName));
		}
		example.orderBy("role_name desc");
		List<PubRole> roles = this.roleService.selectByExample(example);
		return roles;
	}
}
